import { createContext, useContext, Context, MutableRefObject } from "react";
import type { BasicTableProps, TableActionType } from "../types/table";

// Define the context type
interface Instance extends TableActionType {
	wrapRef: MutableRefObject<HTMLElement | null>;
	getBindValues: () => BasicTableProps;
}

// Create a context with undefined as default value
let TableContext: Context<Instance>;

export function createTableContext(val: Instance): Context<Instance> {
	TableContext = createContext(val);
	return TableContext;
}
// Custom hook to use the table context
export function useTableContext(): Instance {
	const context = useContext(TableContext);
	if (!context) {
		throw new Error("useTableContext must be used within a TableProvider");
	}
	return context;
}
